package com.anti.modular.sys.user.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.anti.jdbc.DBUtils;
import com.anti.modular.sys.user.param.SysUserParam;
import com.anti.modular.sys.user.service.SysUserDataScopeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @Description TODO
 * @Author anTi
 * @Date 2021-3-11
 */
@Service
public class SysUserDataScopeServiceImpl implements SysUserDataScopeService {
    @Resource
    DBUtils dbUtils;

    @Override
    public void deleteUserDataScopeListByOrgIdList(List<Long> orgIdList) {
        HashMap<Object, Object> map = CollectionUtil.newHashMap();
        String sql="delete from sys_user_data_scope where org_id in (:ids)";
        map.put("ids",orgIdList);
        dbUtils.execSql(sql,map);
    }

    @Override
    public void deleteUserDataScopeListByUserId(Long userId) {
        String sql="delete from sys_user_data_scope where user_id=?";
        dbUtils.execSql(sql,userId);
    }

    @Override
    public void saveGrantData(SysUserParam sysUserParam) {
        Long userId = sysUserParam.getId();
        //删除所拥有数据
        String removeSql="delete from sys_user_data_scope where user_id=?";
        dbUtils.execSql(removeSql,userId);
        //授权数据
        String insertSql="insert into sys_user_data_scope (user_id,org_id) values (?,?)";
        sysUserParam.getGrantOrgIdList().forEach(orgId -> {
            dbUtils.execSql(insertSql,userId,orgId);
        });
    }

    @Override
    public List<Long> getUserDataScopeIdList(Long uerId) {
        String sql="select org_id from sys_user_data_scope where user_id=?";
        List<Long> userDataScopeIdList = dbUtils.query(sql, Long.class, uerId);
        return userDataScopeIdList;
    }
}
